﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ClockwiseClient
{
    public enum EnumFilter
    {
        FILTER_ZHONGGUO = 0,    // 中国板
        FILTER_HAOHAN,  // 好汉板
        FILTER_BAOZI,   // 豹子板
        FILTER_HOUGU,   // 猴股板
        FILTER_ZIXUAN,  // 自选板
        FILTER_HISTORY, // 历史回顾
        FILTER_AG,  // A股
        FILTER_BG,  // B股
        FILTER_HZB, // 沪主板
        FILTER_SZB, // 深主板
        FILTER_CYB, // 创业板
        FILTER_KCB, // 科创板
        FILTER_ZXB, // 中小板
        FILTER_ST,  // ST板
        FILTER_JJ,  // 基金
        FILTER_ZS,  // 指数
        FILTER_QT,  // 其他

        FILTER_COUNT,
        FILTER_INVALID = -1
    }

    public class ClassFilter
    {
        protected List<ClassFilterItem> lstFilter;
        protected ClassSorter classSorter;
        protected List<String> lstCode;

        public ClassFilter()
        {
            lstFilter = new List<ClassFilterItem>();
            classSorter = null;
        }

        protected void Load(string filename)
        {
            try
            {
                lstCode = new List<String>();
                StreamReader reader = new StreamReader(filename);
                while (true)
                {
                    string code = reader.ReadLine();
                    if (code.Length == 0) break;
                    if (code.Length == 8)
                    {
                        lstCode.Add(code);
                    }
                }
                reader.Close();
            }
            catch (Exception)
            {
            }
        }

        protected void Save(string filename)
        {
            try
            {
                StreamWriter writer = new StreamWriter(filename);
                foreach (string code in lstCode)
                {
                    writer.WriteLine(code);
                }
                writer.Close();
            }
            catch (Exception)
            {
            }
        }

        public int GetCount()
        {
            return lstFilter.Count;
        }

        public int GetIndex(int position)
        {
            if (position < 0 || position >= lstFilter.Count) return -1;
            ClassFilterItem item;
            if (classSorter == null)
                item = lstFilter[position];
            else
                return classSorter.GetIndex(position);
            return item.position;
        }

        public void SetSorter(EnumSorter sorter, bool sortDir)
        {
            switch (sorter) {
                case EnumSorter.SORTER_CODE:
                    classSorter = new ClassSorterCode(sortDir);
                    break;
                case EnumSorter.SORTER_NAME:
                    classSorter = new ClassSorterName(sortDir);
                    break;
                case EnumSorter.SORTER_PRICE:
                    classSorter = new ClassSorterPrice(sortDir);
                    break;
                case EnumSorter.SORTER_DIFF:
                    classSorter = new ClassSorterDiff(sortDir);
                    break;
                case EnumSorter.SORTER_CHANGE:
                    classSorter = new ClassSorterChange(sortDir);
                    break;
                case EnumSorter.SORTER_BUY:
                    classSorter = new ClassSorterBuy(sortDir);
                    break;
                case EnumSorter.SORTER_SELL:
                    classSorter = new ClassSorterSell(sortDir);
                    break;
                case EnumSorter.SORTER_SHOT:
                    classSorter = new ClassSorterShot(sortDir);
                    break;
                case EnumSorter.SORTER_VOLUME:
                    classSorter = new ClassSorterVolume(sortDir);
                    break;
                case EnumSorter.SORTER_AMOUNT:
                    classSorter = new ClassSorterAmount(sortDir);
                    break;
                case EnumSorter.SORTER_PRECLOSE:
                    classSorter = new ClassSorterPreclose(sortDir);
                    break;
                case EnumSorter.SORTER_OPEN:
                    classSorter = new ClassSorterOpen(sortDir);
                    break;
                case EnumSorter.SORTER_HIGH:
                    classSorter = new ClassSorterHigh(sortDir);
                    break;
                case EnumSorter.SORTER_LOW:
                    classSorter = new ClassSorterLow(sortDir);
                    break;
                case EnumSorter.SORTER_AMPLITUDE:
                    classSorter = new ClassSorterAmplitude(sortDir);
                    break;
                default:
                    classSorter = null;
                    break;
            }
            if (classSorter != null) classSorter.Builder(lstFilter);
        }
    }

    public class ClassFilterItem
    {
        public int position;
        public string symbol;
    }

    // 中国
    public class ClassFilterZhongGuo : ClassFilter
    {
        public bool Accept(int i, String name)
        {
            if (name.StartsWith("中"))  // 原来是中国板块，现改成中字头，最近超了一波行情。
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 豹子
    public class ClassFilterBaoZi : ClassFilter
    {
        public bool Accept(int i, String code)
        {
            int baozi = int.Parse(code.Substring(3));
            int gewei = baozi % 10;
            int shiwei = (baozi % 100) / 10;
            int baiwei = baozi / 100;
            if (gewei == shiwei && gewei == baiwei)
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 好汉
    public class ClassFilterHaoHan : ClassFilter
    {
        public ClassFilterHaoHan()
        {
            Load(ClassStock.strWorkDir + @"\UserData\haohan.txt");
        }

        public bool Accept(int i, String symbol)
        {
            foreach (String code in lstCode)
            {
                if (symbol.Equals(code))
                {
                    ClassFilterItem item = new ClassFilterItem();
                    item.position = i;
                    lstFilter.Add(item);
                    return true;
                }
            }
            return false;
        }
    }

    // 猴股
    public class ClassFilterHouGu : ClassFilter
    {
        public ClassFilterHouGu()
        {
            Load(ClassStock.strWorkDir + @"\UserData\monkey.txt");
        }

        public bool Accept(int i, String symbol)
        {
            foreach (String code in lstCode)
            {
                if (symbol.Equals(code))
                {
                    ClassFilterItem item = new ClassFilterItem();
                    item.position = i;
                    lstFilter.Add(item);
                    return true;
                }
            }
            return false;
        }
    }

    // 自选
    public class ClassFilterZiXuan : ClassFilter
    {
        public ClassFilterZiXuan()
        {
            Load(ClassStock.strWorkDir + @"\UserData\zixuangu.txt");
        }

        public bool Accept(int i, String symbol)
        {
            foreach(String code in lstCode) 
            {
                if(symbol.Equals(code)) {
                    ClassFilterItem item = new ClassFilterItem();
                    item.position = i;
                    item.symbol = symbol;
                    lstFilter.Add(item);
                    return true;
                }
            }
            return false;
        }

        public bool Include(string symbol)
        {
            foreach (String code in lstCode)
            {
                if (symbol.Equals(code))
                {
                    return true;
                }
            }
            return false;
        }

        public void Update(String symbol, List<ClassStockItem> lstClassStockItem)
        {
            bool remove = false;
            foreach (String code in lstCode)
            {
                if (symbol.Equals(code))
                {
                    lstCode.Remove(symbol);
                    remove = true;
                    break;
                }
            }
            if (!remove)
            {
                lstCode.Add(symbol);
                int i = 0;
                foreach (ClassStockItem sitem in lstClassStockItem)
                {
                    if (sitem.symbol.Equals(symbol))
                    {
                        ClassFilterItem item = new ClassFilterItem();
                        item.position = i;
                        item.symbol = symbol;
                        lstFilter.Add(item);
                        break;
                    }
                    i++;
                }
            }
            else
            {
                foreach (ClassFilterItem item in lstFilter)
                {
                    if (item.symbol.Equals(symbol))
                    {
                        lstFilter.Remove(item);
                        break;
                    }
                }
            }
            Save(ClassStock.strWorkDir + @"\UserData\zixuangu.txt");
        }
    }

    // 历史回顾
    public class ClassFilterHistory : ClassFilter
    {
        public ClassFilterHistory()
        {
            Load(ClassStock.strWorkDir + @"\UserData\history.txt");
        }

        public bool Accept(int i, String symbol)
        {
            foreach (String code in lstCode)
            {
                if (symbol.Equals(code))
                {
                    ClassFilterItem item = new ClassFilterItem();
                    item.position = i;
                    lstFilter.Add(item);
                    return true;
                }
            }
            return false;
        }

        public void Upadate(String symbol, List<ClassStockItem> lstClassStockItem)
        {
            int i = 0;
            foreach (String code in lstCode)
            {
                if (symbol.Equals(code))
                {
                    if (i > 0)
                    {
                        lstCode.Remove(symbol);
                        break;
                    }
                    else
                    {
                        return;
                    }
                }
                i++;
            }
            lstCode.Insert(0, symbol);
            i = 0;
            foreach (ClassStockItem sitem in lstClassStockItem)
            {
                if (sitem.symbol.Equals(symbol))
                {
                    ClassFilterItem item = new ClassFilterItem();
                    item.position = i;
                    item.symbol = symbol;
                    lstFilter.Insert(0, item);
                    break;
                }
                i++;
            }
            Save(ClassStock.strWorkDir + @"\UserData\history.txt");
        }
    }

    // A股
    public class ClassFilterAG : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz0") || symbol.StartsWith("sz3") || symbol.StartsWith("sh6"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // B股
    public class ClassFilterBG : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz2") || symbol.StartsWith("sh9"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 深主板
    public class ClassFilterSzb : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz000") || symbol.StartsWith("sz001"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 沪主板
    public class ClassFilterHzb : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sh6") && !symbol.StartsWith("sh688"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 创业板
    public class ClassFilterCyb : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz30"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 科创板
    public class ClassFilterKcb : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sh688"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 中小板
    public class ClassFilterZxb : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz002") || symbol.StartsWith("sz003"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // ST
    public class ClassFilterST : ClassFilter
    {
        public bool Accept(int i, String name)
        {
            if (name.IndexOf("ST") >= 0)
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 基金
    public class ClassFilterJiJin : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz1") || symbol.StartsWith("sh5"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 指数
    public class ClassFilterZhiShu : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            if (symbol.StartsWith("sz9") || symbol.StartsWith("sh0"))
            {
                ClassFilterItem item = new ClassFilterItem();
                item.position = i;
                lstFilter.Add(item);
                return true;
            }
            return false;
        }
    }

    // 其他
    public class ClassFilterOther : ClassFilter
    {
        public bool Accept(int i, String symbol)
        {
            ClassFilterItem item = new ClassFilterItem();
            item.position = i;
            lstFilter.Add(item);
            return true;
        }
    }

}
